using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Wintellect.Touch
{
    public class ImageElementResizingBehavior : BaseResizingBehavior<Image>
    {
        #region Behavior Overrides

        protected override void OnAttached()
        {
            base.OnAttached();
            AssociatedObject.ImageOpened += HandleAssociatedObjectImageOpened;
        }

        protected override void OnDetaching()
        {
            AssociatedObject.ImageOpened -= HandleAssociatedObjectImageOpened;

            base.OnDetaching();
        } 

        #endregion

        #region Event Handlers

        private void HandleAssociatedObjectImageOpened(Object sender, RoutedEventArgs e)
        {
            if (ResetScaleAndTranslationOnImageOpened)
            {
                ResetScaleAndTranslation();
            }
        } 

        #endregion

        #region ResetScaleAndTranslationOnImageOpened DP

        /// <summary>
        /// Gets or sets a value indicating whether the to reset the scale and translation values when an image is opened (when the image source is changed).
        /// </summary>
        /// <value>
        /// 	<c>true</c> if [reset scale and translation on image opened]; otherwise, <c>false</c>.
        /// </value>
        public Boolean ResetScaleAndTranslationOnImageOpened
        {
            get { return (Boolean)GetValue(ResetScaleAndTranslationOnImageOpenedProperty); }
            set { SetValue(ResetScaleAndTranslationOnImageOpenedProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ResetScaleAndTranslationOnImageOpened.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ResetScaleAndTranslationOnImageOpenedProperty =
            DependencyProperty.Register("ResetScaleAndTranslationOnImageOpened", typeof(Boolean), typeof(ImageElementResizingBehavior), new PropertyMetadata(true));

        #endregion        
    }
}